package com.example.collect.tool;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * 替换文件工具
 * 从指定目录A，复制到指定目录B，替换同名文件
 *
 * @author admin
 */
public class CopyDemo {

    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("origin path:");
        String originPath = scanner.nextLine();
        System.out.println("target path:");
        String targetPath = scanner.nextLine();
        List<String> targetPaths = Arrays.stream(targetPath.split(";")).filter(e -> !e.trim().isEmpty())
                .collect(Collectors.toList());

        System.out.println(String.format("target path is : %s", Arrays.toString(targetPaths.toArray())));
        while (true) {
            System.out.println("file name:");
            String fileName = scanner.nextLine();
            if ("exit".equalsIgnoreCase(fileName)) {
                System.out.println("closing...");
                break;
            }
            Path sourcePath = Paths.get(originPath, fileName);
            if (!sourcePath.toFile().exists()) {
                System.err.println("file is not exist");
                continue;
            }
            if (!sourcePath.toFile().isFile()) {
                System.err.println("file is not file");
                continue;
            }

            int i = 0;
            for (String targetPathItem : targetPaths) {
                i++;
                System.out.println(String.format("current: %s total: %s  doing...", i, targetPaths.size()));
                Path target = findFile(Paths.get(targetPathItem), fileName);
                if (target == null) {
                    System.err.println(String.format("current: %s total: %s  not found target file", i, targetPaths.size()));
                } else {
                    Files.copy(sourcePath, target, StandardCopyOption.REPLACE_EXISTING);
                    System.out.println(String.format("current: %s total: %s  success...", i, targetPaths.size()));
                }
            }
        }
        scanner.close();
    }

    private static Path findFile(Path path, String fileName) throws IOException {
        if (!path.toFile().exists()) {
            return null;
        }
        if (path.toFile().isFile()) {
            if (path.toFile().getName().equalsIgnoreCase(fileName)) {
                return path;
            } else {
                return null;
            }
        } else {
            List<Path> collect = Files.list(path).collect(Collectors.toList());
            for (Path subPath : collect) {
                Path findPath = findFile(subPath, fileName);
                if (findPath != null) {
                    return findPath;
                }
            }
            return null;
        }
    }
}
